<template>
  <div v-if="errorStatistics.length > 0" class="error-statistics-container">
    <div class="error-statistics-flex-container">
      <!-- 柱状图 -->
      <div class="chart-container">
        <div ref="errorChart" style="width: 100%; height: 400px;"></div>
      </div>

      <!-- 错误统计表格 -->
      <div class="chart-section">
        <el-divider content-position="center">错误统计</el-divider>
        <el-table :data="errorStatistics" border stripe style="width: 100%; margin-top: 15px;">
          <el-table-column prop="errorCode" label="错误类型" width="120"></el-table-column>
          <el-table-column prop="description" label="错误描述" min-width="300"></el-table-column>
          <el-table-column prop="count" label="数量" width="100" align="center">
            <template #default="{row}">
              <el-tag type="danger">{{ row.count }}</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue'
import * as echarts from 'echarts'

// 定义错误统计项接口
interface ErrorStatItem {
  errorCode: string
  count: number
  description: string
}

const props = defineProps<{
  errorStatistics: ErrorStatItem[]
}>()

// 图表实例
let chartInstance: echarts.ECharts | null = null
const errorChart = ref<HTMLElement | null>(null)

// 监听错误统计数据变化，更新图表
watch(() => props.errorStatistics, (newVal) => {
  if (newVal.length > 0) {
    nextTick(() => {
      initErrorChart()
    })
  }
}, { deep: true })

// 初始化图表
const initErrorChart = () => {
  if (chartInstance) {
    chartInstance.dispose()
  }
  
  if (!errorChart.value) return

  chartInstance = echarts.init(errorChart.value)
  
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    xAxis: {
      type: 'category',
      data: props.errorStatistics.map(item => item.errorCode),
      axisLabel: {
        rotate: 45
      }
    },
    yAxis: {
      type: 'value'
    },
    series: [{
      data: props.errorStatistics.map(item => item.count),
      type: 'bar',
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#83bff6' },
          { offset: 0.5, color: '#188df0' },
          { offset: 1, color: '#188df0' }
        ])
      }
    }]
  }

  chartInstance.setOption(option)
}

// 组件挂载时添加窗口大小变化监听
onMounted(() => {
  window.addEventListener('resize', handleChartResize)
})

// 组件卸载时清理资源
onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose()
    chartInstance = null
  }
  window.removeEventListener('resize', handleChartResize)
})

const handleChartResize = () => {
  if (chartInstance) {
    chartInstance.resize()
  }
}
</script>

<style scoped>
.error-statistics-container {
  margin-top: 30px;
}

.error-statistics-flex-container {
  display: flex;
  gap: 20px;
}

.chart-container {
  flex: 1;
  min-width: 0;
  margin-top: 20px;
}

.chart-section {
  flex: 1;
  min-width: 0;
  margin-top: 20px;
}
</style>